//
//  CollectionViewController.m
//  LittleFairyPoet
//
//  Created by rimi on 15/9/9.
//  Copyright (c) 2015年 周林. All rights reserved.
//

#import "CollectionViewController.h"
#import "ZLDetailPoemViewController.h"
#define COLLECTED_POEM_PATH NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject

static NSString *const kUITableViewCellIdentifier = @"collectCellIdentifier";

@interface CollectionViewController ()<UITextFieldDelegate,UITableViewDataSource, UITableViewDelegate> {
    NSMutableArray *_collectionDataSource;
    NSString *_collectedPoemPath;
}

@property (nonatomic, strong) UIImageView * searchImageView;
@property (nonatomic, strong) UITextField * searchTextField;
@property (nonatomic, strong) UITableView * collectionTabView;
- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation CollectionViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
#pragma mark - private methods
- (void)initializeDataSource {
    _collectedPoemPath = [COLLECTED_POEM_PATH stringByAppendingString:@"/CollectedPoem.plist"];
    _collectionDataSource = [NSMutableArray arrayWithContentsOfFile:_collectedPoemPath];
}
- (void)initializeUserInterface {
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleLable.text = @"收藏";
    [self.view addSubview:self.searchImageView];
    [self.view addSubview:self.searchTextField];
    [self.view addSubview:self.collectionTabView];
}
#pragma mark - responds events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)respondsToSearchButton {
    
}
#pragma mark---协议UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _collectionDataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kUITableViewCellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.text = _collectionDataSource[indexPath.row][@"title"];
    cell.detailTextLabel.text = _collectionDataSource[indexPath.row][@"zuozhe"];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
#pragma mark - UITableViewDataSource
//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

//用户点击了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLDetailPoemViewController * detailVC = [[ZLDetailPoemViewController alloc]init];
    detailVC.poemDic = _collectionDataSource[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - getter
- (UIImageView *)searchImageView {
    if (!_searchImageView) {
        _searchImageView = ({
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:DHFlexibleFrame(CGRectMake(0, 64, 320, 40), YES)];
            imageView.image = IMAGE_CONTENT(@"搜索框.png");
            imageView.backgroundColor = [UIColor colorWithRed:212/255.0 green:212/255.0 blue:212/255.0 alpha:1];
            imageView;
        });
    }
    return _searchImageView;
}
- (UITextField *)searchTextField {
    if (!_searchTextField) {
        _searchTextField = ({
            UITextField * textField = [[UITextField alloc]initWithFrame:DHFlexibleFrame(CGRectMake(40, 69, 300, 30), YES)];
            UIColor *color = [UIColor lightGrayColor];
            textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入作品名或作者名字" attributes:@{NSForegroundColorAttributeName: color}];
            textField.returnKeyType = UIReturnKeySearch;
            textField.leftViewMode = UITextFieldViewModeAlways;
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = DHFlexibleFrame(CGRectMake(20, 0, 20, 20), YES);
            [button setImage:IMAGE_CONTENT(@"放大镜.png") forState:UIControlStateNormal];
            [button addTarget:self action:@selector(respondsToSearchButton) forControlEvents:UIControlEventTouchUpInside];
            textField.leftView = button;
            textField.textColor = color;
            textField.backgroundColor = [UIColor clearColor];
            textField.delegate = self;
            textField;
        });
    }
    return _searchTextField;
}

- (UITableView *)collectionTabView {
    if (!_collectionTabView) {
        _collectionTabView = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:DHFlexibleFrame(CGRectMake(0, 104, ORIGIN_WIDTH, ORIGIN_HEIGHT-104), NO) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableFooterView = [UIView new];
            tableview.pagingEnabled = NO;
            tableview.separatorColor = [UIColor blackColor];
            tableview.backgroundColor = ZLColorRGBA(232, 233, 232, 1);
            tableview;
        });
    }
    return _collectionTabView;
}
@end
